package com.taplinker.client.crm.adapter;

import java.util.List;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.taplinker.client.R;

public class ExpandableAdapter extends BaseExpandableListAdapter {
	private List<String> groupArray;
	private List<List<String>> childArray;
	private Context mContext;

	public ExpandableAdapter(List<String> groupArray,
			List<List<String>> childArray, Context context) {
		this.groupArray = groupArray;
		this.childArray = childArray;
		mContext = context;
	}

	@Override
	public Object getChild(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return childArray.get(arg0).get(arg1);
	}

	@Override
	public long getChildId(int arg0, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		String string = childArray.get(groupPosition).get(childPosition);
		return getGenericView(string);
	}
	public  TextView getGenericView(String string)  
    {  
        // Layout parameters for the ExpandableListView   
        AbsListView.LayoutParams layoutParams = new  AbsListView.LayoutParams(  
                ViewGroup.LayoutParams.FILL_PARENT, 64 );  
        TextView text = new  TextView(mContext);  
        text.setLayoutParams(layoutParams);  
        // Center the text vertically   
        text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
        // Set the text starting position   
        text.setPadding(36 , 0 , 0 , 0 );  
        text.setTextColor(mContext.getResources().getColor(R.color.black));
        text.setText(string);  
        return  text;  
    }  
	@Override
	public int getChildrenCount(int arg0) {
		// TODO Auto-generated method stub
		return childArray.get(arg0).size();
	}

	@Override
	public Object getGroup(int arg0) {
		// TODO Auto-generated method stub
		return groupArray.get(arg0);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return groupArray.size();
	}

	@Override
	public long getGroupId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getGroupView(int arg0, boolean isExpanded, View convertView,
			ViewGroup arg3) {
		Holder holder = null;
		if (convertView == null) {
			holder = new Holder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.expandlistview_title, null);
			holder.tv = (TextView) convertView.findViewById(R.id.expand_title);
			holder.iv = (ImageView) convertView.findViewById(R.id.tubiao);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		holder.tv.setText(groupArray.get(arg0));
		if (isExpanded)
			holder.iv.setBackgroundResource(R.drawable.crm_eidt_industry_downarrow);
		else
			holder.iv.setBackgroundResource(R.drawable.selector_edit_industry_category);
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}

	class Holder {
		TextView tv;
		ImageView iv;
	}
}
